package com.itheima.xinxi.system.controller;

import com.itheima.xinxi.system.domain.Student;
import com.itheima.xinxi.system.domain.Teacher;
import com.itheima.xinxi.system.domain.Teacher;
import com.itheima.xinxi.system.service.TeacherService;

import java.util.Scanner;

/**
 * 老师用户交互类
 */
public class TeacherController {
    private TeacherService teacherService= new TeacherService();
    private Scanner sc= new Scanner(System.in);
    
    
    // 入口
    public void start(){
        xuanze:
        while (true) {
            System.out.println("--------欢迎来到老师管理系统--------");
            System.out.println("1 添加老师");
            System.out.println("2 删除老师");
            System.out.println("3 修改老师");
            System.out.println("4 查看老师");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    // System.out.println("添加老师");//模拟我已经把这个做完了
                    this.addTeacher();
                    break;
                case 2:
                    //System.out.println("删除老师");//模拟我已经把这个做完了
                    this.deleteTeacherById();
                    break;
                case 3:
                    //System.out.println("修改老师");//模拟我已经把这个做完了
                    this.updateTeacher();
                    break;
                case 4:
                    // System.out.println("查看老师");//模拟我已经把这个做完了
                    this.findAllTeachers();
                    break;

                case 5:
                    System.out.println("感谢使用");//模拟我已经把这个做完了
                    //break xuanze;

                    return;

                default:
                    System.out.println("输入有误");//模拟我已经把这个做完了
                    break;
            }
        }
    }




    //增
    private void addTeacher() {
        //键盘录入数据
        System.out.println("请输入学号");
        String id;
        while (true) {
            //处理学号重复问题
            id = sc.next();
            //调用service 判断学号是否存在的方法
            boolean flag = teacherService.panduan(id);
            if (flag) {
                System.out.println("重录");
            } else {
                break;
            }
        }

        Teacher teacher = getTeacher(id);
        teacherService.addTeacher(teacher);
        System.out.println("成功!");


    }
    //删
    private void deleteTeacherById() {
        String id = getId();
        teacherService.deleteTeacherById(id);
        System.out.println("成功!");
    }
    //改 

    private void updateTeacher() {
        String id = getId();
        Teacher teacher = getTeacher(id);
        teacherService.updateTeacher(id,teacher);
        System.out.println("成功!");
    }
    //查
    private void findAllTeachers() {
        Teacher[] teachers = teacherService.findAll();
        if(teachers==null){
            System.out.println("没有");
            return;
        }
        System.out.println("工号\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            //stus[i]
            Teacher teacher = teachers[i];
            if (teacher != null) {
                System.out.println(teacher.getId() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "岁\t" + teacher.getBirthday());
            }
        }

    }
    // 收集 id
    public String getId(){
        //键盘录入数据
        System.out.println("请输入学号");
        String id;
        while (true) {
            //处理学号重复问题
            id = sc.next();
            //调用service 判断学号是否存在的方法
            boolean flag = teacherService.panduan(id);
            if (!flag) {
                System.out.println("重录");
            } else {
                break;
            }
        }

        return id;
    }
    //收集数据
    public Teacher getTeacher(String id){
        System.out.println("请输入姓名");
        //姓名
        String name = sc.next();
        System.out.println("请输入年龄");
        //年龄
        int age = sc.nextInt();
        System.out.println("请输入生日");
        //生日
        String birthday = sc.next();
        //封装数据
        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);
        return teacher;
        
    }
    
    
    
    
}
